// MobileSketcher.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resourceppc.h"
#include "MobileSketcher.h"

#include "SignatureControl.h"

static int GetColourFromCommandId(UINT id)
{
	int colour = RGB(0, 0, 0);

	switch (id)
	{
		case IDC_COLOUR_RED:
			colour = RGB(255, 0, 0);
			break;
			
		case IDC_COLOUR_ORANGE:
			colour = RGB(255,165,0);
			break;

		case IDC_COLOUR_YELLOW:
			colour = RGB(255, 255, 0);
			break;

		case IDC_COLOUR_GREEN:
			colour = RGB(0, 128, 0);
			break;

		case IDC_COLOUR_BLUE:
			colour = RGB(0, 0, 255);
			break;

		case IDC_COLOUR_BROWN:
			colour = RGB(165, 42, 42);
			break;

		case IDC_COLOUR_GRAY:
			colour = RGB(220, 220, 220);
			break;

		case IDC_COLOUR_BLACK:
			colour = RGB(0, 0, 0);
			break;
	}

	return colour;
}

// Message handler for Mobile Sketcher dialog.
INT_PTR CALLBACK MobileSketcherDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	HWND hWndSignature = GetDlgItem(hDlg, IDC_PICTURE);

	switch (message)
    {
        case WM_INITDIALOG:
            {
                // Create a Done button and size it.  
                SHINITDLGINFO shidi;
				SHMENUBARINFO mbi;
                shidi.dwMask = SHIDIM_FLAGS;
                shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
                shidi.hDlg = hDlg;
                SHInitDialog(&shidi);

				// Create a menu bar
                memset(&mbi, 0, sizeof(SHMENUBARINFO));
				mbi.cbSize     = sizeof(SHMENUBARINFO);
				mbi.hwndParent = hDlg;
				mbi.nToolBarId = IDR_MENU;
				mbi.hInstRes   = GetModuleHandle(NULL);
				mbi.dwFlags = SHCMBF_HMENU;
	            SHCreateMenuBar(&mbi);
            }
            return (INT_PTR)TRUE;

		case WM_CTLCOLORSTATIC:
			{
				// Alter the background colour of the
				// labels
				int id = GetWindowLong((HWND)lParam, GWL_ID);
				HBRUSH hbrBackground = NULL;

				switch (id)
				{
					case IDC_COLOUR_RED:
					case IDC_COLOUR_ORANGE:
					case IDC_COLOUR_YELLOW:
					case IDC_COLOUR_GREEN:
					case IDC_COLOUR_BLUE:
					case IDC_COLOUR_BROWN:
					case IDC_COLOUR_GRAY:
					case IDC_COLOUR_BLACK:
						hbrBackground = CreateSolidBrush(GetColourFromCommandId(id));
						break;
				}

				return (INT_PTR)hbrBackground;
			}
			break;

        case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDM_CLEAR:
					Signature_Clear(hWndSignature);
					break;

				case IDM_PICTURE:
					{
						TCHAR szFilenameBuffer[MAX_PATH] = {0};
						OPENFILENAMEEX ofn = {0};
						ofn.lStructSize = sizeof(ofn);
						ofn.hwndOwner = hDlg;
						ofn.lpstrFile = szFilenameBuffer;
						ofn.nMaxFile = MAX_PATH;
						ofn.lpstrTitle = _T("Select image to draw on");
						ofn.ExFlags = OFN_EXFLAG_THUMBNAILVIEW | OFN_EXFLAG_HIDEDRMFORWARDLOCKED;

						if (GetOpenFileNameEx(&ofn))
						{
							Signature_Load(hWndSignature, ofn.lpstrFile);
						}
					}
					break;

				case IDM_SAVE:
					{
						TCHAR szFilenameBuffer[MAX_PATH] = {0};
						OPENFILENAME ofn = {0};
						ofn.lStructSize = sizeof(ofn);
						ofn.hwndOwner = hDlg;
						ofn.lpstrFilter = _T("Bitmap (*.bmp)\0*.bmp\0All files (*.*)\0*.*\0");
						ofn.nFilterIndex = 1;
						ofn.lpstrFile = szFilenameBuffer;
						ofn.nMaxFile = MAX_PATH;
						ofn.lpstrDefExt = _T("bmp");
						ofn.lpstrTitle = _T("Save Picture As");
						ofn.Flags = OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY;

						if (GetSaveFileName(&ofn))
						{
							Signature_Save(hWndSignature, ofn.lpstrFile);
						}
					}
					break;

				case IDC_COLOUR_RED:
				case IDC_COLOUR_ORANGE:
				case IDC_COLOUR_YELLOW:
				case IDC_COLOUR_GREEN:
				case IDC_COLOUR_BLUE:
				case IDC_COLOUR_BROWN:
				case IDC_COLOUR_GRAY:
				case IDC_COLOUR_BLACK:
					if (HIWORD(wParam) == STN_CLICKED)
					{
						// Tell the signature control the new colour
						// it should paint with
						Signature_SetColour(hWndSignature, GetColourFromCommandId(LOWORD(wParam)));
					}
					break;

				case IDOK:
	                EndDialog(hDlg, LOWORD(wParam));
					return (INT_PTR)TRUE;
            }
            break;

        case WM_CLOSE:
            EndDialog(hDlg, message);
            return (INT_PTR)TRUE;

    }
    return (INT_PTR)FALSE;
}

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPTSTR    lpCmdLine,
                   int       nCmdShow)
{
	InitSignatureControl();

	return DialogBox(GetModuleHandle(NULL), (LPCTSTR)IDD_MOBILESKETCHER, NULL, MobileSketcherDlg);
}
